// ignore_for_file: file_names, must_be_immutable

import 'package:flutter/material.dart';

class MyApp11 extends StatelessWidget {
  MyApp11({super.key});

  List<Widget> getList() {
    //写法1
    // List<Widget> list = [];
    // for (var i = 0; i < 20; i++) {
    //   list.add(Container(
    //     width: double.infinity,
    //     height: 100,
    //     margin: EdgeInsets.all(10),
    //     alignment: Alignment.center,
    //     decoration: BoxDecoration(
    //       borderRadius: BorderRadius.circular(10),
    //       color: Colors.red,
    //     ),
    //     child: Text(
    //       '第$i个列表项',
    //       style: TextStyle(color: Colors.white),
    //     ),
    //   ));
    // }
    // return list;

    // 写法2
    // return List.generate(20, (i) {
    //   return Container(
    //     width: double.infinity,
    //     height: 100,
    //     margin: EdgeInsets.all(10),
    //     alignment: Alignment.center,
    //     decoration: BoxDecoration(
    //       borderRadius: BorderRadius.circular(10),
    //       color: Colors.red,
    //     ),
    //     child: Text(
    //       '第$i个列表项',
    //       style: TextStyle(color: Colors.white),
    //     ),
    //   );
    // });

    return [
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
    ].map((int value) {
      return Container(
          width: double.infinity,
          height: 100,
          margin: EdgeInsets.all(10),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            color: Colors.red,
          ),
          child: Text(
            '第${value}个列表项',
            style: TextStyle(color: Colors.white),
          ));
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //1.导航
        appBar: AppBar(
          title: const Text('ListView组件'),
          backgroundColor: Colors.pink,
        ),
        //2.内容
        body: ListView(children: getList()));
  }
}
